--
-- Author: yawei.zhu
-- Date: 2018-07-16 14:14:01
--
-- ChampionCopyController
--

local NewActivityDungeonsRoundPanel = import("..views.NewActivityDungeonsRoundPanel")

-- local ChampionCopyView = import("..views.ChampionCopyView")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ControllerBase)

local knockOutDesc = {L("throne_game64"), L("throne_game32"), L("lua_code_text_405"), L("lua_code_text_177"), L("lua_code_text_406"), L("lua_code_text_270") }

function ClassRef:ctor(data)
    self.super.ctor(self, data)
end

function ClassRef:init()
    self._model = app:getInst("NewActivityDungeonsModel")
end

function ClassRef:removeListeners()
    self.super.removeListeners(self)
end

function ClassRef:onEvent(eventName, data)
    if eventName == "onMapChanged" then
        if self._model:isChampionMap() then
            self._model:reqChampionInfo()
        else
            if self._roundView and not cc.isDead(self._roundView) then 
                self._roundView:removeFromParent()
            end
            self._roundView = nil
        end
    end
end

function ClassRef:listEvent()
    return {
        "onMapChanged"
    }
end

-- 服务器下发 进入战斗的时候，关闭界面

-- 显示倒计时
function ClassRef:showRoundCountDown()
    if not self._pvpModel then self._pvpModel = app:getInst("PvpModel") end

    if not self._roundView or cc.isDead(self._roundView) then 
        self._roundView = NewActivityDungeonsRoundPanel.create()
        self._roundView:setPosition( cc.p( display.width/2 , display.height/2 ) )
        self._roundView:onCloseView(function() self:removeChild(self._roundView, {isFade = false}) end)
        self._roundView:onOperate(handler(self, self.onOperate))
        self:addChild(self._roundView, {isFade = false} )
    end

    self:updateRoundCountDown()
end

-- data 类型
-- title
-- time
-- formatTime
-- btnName
function ClassRef:updateRoundCountDown()
    if cc.isDead(self._roundView) then 
        self._roundView = nil
        return 
    end

    local data = {}

    if self._pvpModel:checkMyTeamIsKnockOut() then -- 如果我被淘汰了
        data.title = L("game_over")
        data.time = 0
        data.formatTime = L("lua_code_text_10") 
        data.btnName = L("lua_code_text_389")
    else
        -- 获得了冠军
        if self._pvpModel:checkIsChampion() then
            data.title = L("game_over")
            data.time = 0
            data.formatTime = L("lua_code_text_132") 
            data.btnName = L("lua_code_text_389")
        else
            local day = app:getWeekDay()
            local rankInfo = self._pvpModel:getThroneRankInfo()
            local nextRound = rankInfo.currentRound + 1
            local matchConfig = GD:queryThroneByDay(day)
            local szTime = matchConfig["battleTime"..nextRound]
            
            local function setFinalData()
                data.title = L("lua_code_text_270")
                data.time = 0
                data.formatTime = L("lua_code_text_30") 
                data.btnName = L("lua_code_text_389")
            end

            local function setNormalData(intervalTime)
                data.time = app.session:getServerTime() + intervalTime
                data.formatTime = L("lua_code_text_269")  
                data.btnName = L("lua_code_text_389")
    
                data.title = string.format( L("lua_code_text_528"), nextRound)
                -- 是否在淘汰赛阶段
                local throneConfig = GD:queryThroneByDay(app:getWeekDay())
                if nextRound > tonumber(throneConfig.normalGameNum) then
                    data.title = knockOutDesc[nextRound - tonumber(throneConfig.normalGameNum)]
                end
            end

            if szTime == nil then -- 当前正在进行决赛
                -- 在当前方案下，应该不会进来这里，在当前轮所有的战斗都打完的时候，服务器才会重置并下发round
                setFinalData()
            else
                local beginTime = string.split(szTime, "|")
                local timeDate = app.session:getServerDate()
                local intervalTime = tonumber(beginTime[1]) * 3600 + tonumber(beginTime[2]) * 60 - timeDate.hour * 3600 - timeDate.min * 60 - timeDate.sec
                if intervalTime < 0 then  -- 第nextRound轮的战斗还没打完
                    nextRound = nextRound + 1
                    szTime = matchConfig["battleTime"..nextRound]
                    if szTime == nil then -- 当前正在进行决赛
                        setFinalData()
                    else
                        beginTime = string.split(szTime, "|")
                        intervalTime = tonumber(beginTime[1]) * 3600 + tonumber(beginTime[2]) * 60 - timeDate.hour * 3600 - timeDate.min * 60 - timeDate.sec   
                        setNormalData(intervalTime)
                    end
                else
                    setNormalData(intervalTime)
                end
            end
            
        end  
    end

    dump(data)
    self._roundView:setData( data )
end

-------------------------------------------------------


-------------------------------------------------------
function ClassRef:onOperate(op, data)
    print("ChampionCopyController:onOperate op = " .. op)
    if op == "NewActivity_Round_Btn" then
        print("55555555555555")

        app:sendMsg("PvpChampionController", "openView", self._pvpModel:getThroneInfo())      
    end
end

return ClassRef
